package com.gexiaobao.pigeon.ui.fragment.mine.training

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.model.bean.TrainingSinglePigeonResponse
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentSingleTrainingBinding
import com.gexiaobao.pigeon.ui.adapter.SinglePigeonTrainingAdapter
import com.gexiaobao.pigeon.viewmodel.MyTrainingViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 18:03
 * @Describe    : 单只放飞
 */
class FragmentSingleTraining : BaseFragment<MyTrainingViewModel, FragmentSingleTrainingBinding>() {

    private var trainingId = ""

    private val mAdapter: SinglePigeonTrainingAdapter by lazy { SinglePigeonTrainingAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>
    private var updateTrainingData: ArrayList<TrainingSinglePigeonResponse.SinglePigeonList> = arrayListOf()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.toolbar.initClose(getString(R.string.single_pigeon_fly)) {
            nav().navigateUp()
        }
        trainingId = arguments?.getString("trainingId").toString()
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(context, 5f))
        )
    }

    override fun initData() {
        super.initData()

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getTrainingSinglePigeon(trainingId)
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getTrainingSinglePigeon(trainingId)
        }

        mAdapter.addChildClickViewIds(R.id.train_fly_single_fly_btn)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.train_fly_single_fly_btn -> {
                    mViewModel.updateTrainingPigeon(updateTrainingData[position].id)
                }
            }
        }
    }


    override fun lazyLoadData() {
        super.lazyLoadData()
        loadsir.showLoading()
        mViewModel.getTrainingSinglePigeon(trainingId)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.trainingSinglePigeonData.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
            it.listData?.let { it1 -> updateTrainingData.addAll(it1) }
        }

        mViewModel.updateTrainingTimeResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                loadsir.showLoading()
                mViewModel.getTrainingSinglePigeon(trainingId)
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }
}